package datasource;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Carlos
 * @version 1.0
 * @created 17-ago-2011 03:55:39 p.m.
 */
public class ReadDataSource implements GeneralDataSource {

    public MyImage myImage;

    public ReadDataSource(String path) {
        myImage = new MyImage(path);
        try {
            readImage();
        } catch (Exception ex) {
            Logger.getLogger(ReadDataSource.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getPath() {
        return myImage.getPath();
    }

    public static void readImage() throws Exception {
        int[][] bits;
        int columns;
        int rows;
        String filename = "ByNyR2.bmp";
        BufferedReader infile = new BufferedReader(new FileReader(filename));

        String firstLine = infile.readLine();

        /*if (!firstLine.equals("P1")) {
            throw new IOException("File format error");
        }*/

        StringTokenizer tokens = new StringTokenizer(infile.readLine(), " ");

        columns = Integer.parseInt(tokens.nextToken());
        rows = Integer.parseInt(tokens.nextToken());

        bits = new int[rows][columns];

        for (int i = 0; i < rows; i++) {
            String line = infile.readLine();

            for (int j = 0; j < columns; j++) {
                bits[i][j] = line.charAt(j) - '0';
            }
        }
        for (int i = 0; i < bits.length; i++) {
            for (int j = 0; j < bits[i].length; j++) {                
                System.out.println(bits[i][j]);
            }            
        }
    }
}